#!/bin/bash

GITEE_USER=why0406
GITEE_PROJECT=typora_images
GITEE_PATH=master/images
LOCAL_IMAGE_PATH=/Users/hezhaozhan/hzz/typora_images

encodeurl() {
     local length="${#1}"
     for (( i = 0; i < length; i++))
     do  
         local char="${1:$i:1}"
         case $char in
             [a-zA-Z0-9.~_-:*#/\\])
			    # 不需要转码的字符
				printf '%s' "$char" ;;
             *) 
				echo "$char" | tr -d '\n' | xxd -plain | sed 's/\(..\)/%\1/g' | tr -d '\n' | tr '[a-z]' '[A-Z]';;
         esac
     done
}

cd ${LOCAL_IMAGE_PATH}
dir=`echo $1 | awk -F \/ '{printf "%s",$(NF-1)}'`
file=`echo $1 | awk -F \/  '{printf "%s",$(NF)}'`

# 得到dir中的中文,如果为空则不用进行转码(mac 下需要安装最新的 gawk, 可使用 brew 进行安装)
zh_str=$(echo "${dir}" | /usr/local/bin/awk '{print gensub(/[!-~]/,"","g",$0)}')
if [ "x${zh_str}" != "x" ]
then
	# 对中文和空格等进行转码ASCII
	url=`encodeurl "${dir}"`
else
	url=${dir}
fi

git add ./images/${dir}/${file}
#git add .
git commit -m "add image ./${dir}/${file}"
git push

# blob需要替换为raw
# public库地址
echo https://gitee.com/${GITEE_USER}/${GITEE_PROJECT}/raw/${GITEE_PATH}/${url}/${file}
